﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManWebRole.Models
{
    public class AccountModel
    {
        private SkySRMDBDataDataContext db = new SkySRMDBDataDataContext();

        public IEnumerable<Account> GetAllAccounts()
        {
            return db.Accounts;
        }

        public Account GetAccountById(int id)
        {
            return db.Accounts.SingleOrDefault(a => a.Id == id);
        }

        public void AddAccount(Account account)
        {
            db.Accounts.InsertOnSubmit(account);
            db.SubmitChanges();
        }

        public void DeleteAccount(int id)
        {
            Account account = GetAccountById(id);
            db.Accounts.DeleteOnSubmit(account);
            db.SubmitChanges();
        }

        public Account GetAccountByNamePassword(string username, string password)
        {
            return db.Accounts.SingleOrDefault(a => a.UserName == username && a.Password == password);
        }

        public void UpdateAccount(Account account)
        {
            Account oldAccount = GetAccountById(account.Id);
            oldAccount.Type = account.Type;
            oldAccount.ProjectId = account.ProjectId;
            oldAccount.UserName = account.UserName;
            oldAccount.Password = account.Password;
            oldAccount.Type = account.Type;
            db.SubmitChanges();
        }
    }
}